﻿using DL.Repositories;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace BusinessService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "NguoiDungService" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select NguoiDungService.svc or NguoiDungService.svc.cs at the Solution Explorer and start debugging.
    public class NguoiDungService : INguoiDungService
    {
        public string CapNhatNguoiDung(NguoiDung nguoidungmodel)
        {
            var nguoidungDataTier = new NguoiDungRepository();
            var result = nguoidungDataTier.CapNhatNguoiDung(nguoidungmodel.Id, nguoidungmodel.Email, nguoidungmodel.MatKhau, nguoidungmodel.DienThoai, nguoidungmodel.DiaChi, nguoidungmodel.HoTen);
            return result;
        }
        public NguoiDung Login(string strEmail, string strMatKhau)
        {
            var nguoidungDataTier = new NguoiDungRepository();
            var dataTable = nguoidungDataTier.Login(strEmail, strMatKhau);
            NguoiDung model = new NguoiDung();
            if (dataTable != null && dataTable.Rows.Count == 1)
            {
                foreach (DataRow dr in dataTable.Rows)
                {
                    model.Id = Convert.ToInt64(dr["N_Id"]);
                    model.Email = dr["N_Email"].ToString();
                    model.MatKhau = dr["N_Pass"].ToString();
                    model.DienThoai = dr["N_DienThoai"].ToString();
                    model.TrangThai = Convert.ToInt32(dr["N_TrangThai"]);
                    model.Quyen = Convert.ToInt32(dr["N_Quyen"]);
                    model.DiaChi = dr["N_DiaChi"].ToString();
                    model.HoTen = dr["N_HoTen"].ToString();
                }
                return model;
            }
            return null;
        }

        public List<NguoiDung> DanhSachNguoiDung(int loai = 0)
        {
            var nguoidungDataTier = new NguoiDungRepository();
            var dataTable = nguoidungDataTier.DanhSachNguoiDung(loai);
            var listNguoiDung = new List<NguoiDung>();
            if (dataTable != null)
            {
                foreach (DataRow dr in dataTable.Rows)
                {
                    NguoiDung model = new NguoiDung();
                    model.Id = Convert.ToInt64(dr["N_Id"]);
                    model.Email = dr["N_Email"].ToString();
                    model.MatKhau = dr["N_Pass"].ToString();
                    model.DienThoai = dr["N_DienThoai"].ToString();
                    model.TrangThai = Convert.ToInt32(dr["N_TrangThai"]);
                    model.Quyen = Convert.ToInt32(dr["N_Quyen"]);
                    model.DiaChi = dr["N_DiaChi"].ToString();
                    model.HoTen = dr["N_HoTen"].ToString();
                    model.TenQuyen = dr["TenQuyen"].ToString();
                    listNguoiDung.Add(model);
                }
                return listNguoiDung;
            }
            return null;
        }

        public bool CheckEmail(string strEmail)
        {
            var nguoidungDataTier = new NguoiDungRepository();
            var dataTable = nguoidungDataTier.CheckEmail(strEmail);
            var listNguoiDung = new List<NguoiDung>();
            if (dataTable.Rows.Count != 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        public string DoiMatKhau(long IdNguoiDung, string matkhaucu, string matkhaumoi)
        {
            var nguoidungDataTier = new NguoiDungRepository();
            var result = nguoidungDataTier.DoiMatKhau(IdNguoiDung, matkhaucu, matkhaumoi);
            return result;
        }
    }
}
